import {
    Component, OnInit, EventEmitter, Output, Input, SimpleChanges, OnChanges, DoCheck, ViewChild,
} from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd';
import { tap } from 'rxjs/operators';
import * as moment from 'moment';
@Component({
    selector: 'app-query',
    templateUrl: './query.component.html',
    styleUrls: ['./query.component.less']
})
export class QueryComponent implements OnInit {

    constructor(
        private http: _HttpClient,
        public msg: NzMessageService,
    ) {
    }
    @Input()
    headerDate: any[]; // 初始化字段
    @Output()
    getQuery: EventEmitter<any> = new EventEmitter(); // 新增、修改按钮方法输出

    @Output()
    resetButton: EventEmitter<any> = new EventEmitter(); // 定义一个输出属性，按钮点击事件，非必选

    // 初始化方法
      universalStar = []
    ngOnInit() {
       this.universal.forEach(i=>{
           i.field = this.headerDate
       })
       this.universalStar = this.universal

    }

    /* 运算符 */
    universal= [
    {

        field: [  ],
       logic:  {label:"并且",value:'AND'},
       isLogic:false,
       queryList: [{
           logic: {label:"并且",value:'AND'},
            operator: {label:"=",value:'EQ'},
            field: null,
            value:null,
            hotTags:[],
            type:'input',
            select:[],
            isLogic:false
        }]

    }
]

logic = [
   {label:"并且",value:'AND'},
   {label:"或者",value:'OR'},
]



  handleChange(event,tag,hotTags,queryList): void {

   hotTags.forEach(i=>{
          i.check = false;
          if(tag.label === i.label){
             i.check = true;
             queryList.operator.value = i.value;
             queryList.operator.label = i.label;
          }
      })

  }
  changeLogic(it,j){
     j.logic.label = it.label
     j.logic.value = it.value

  }

  ChangeOperator(it,obj){
       obj.field.forEach(element => {
           if(element.value === it.field){
                it.hotTags = element.hotTags
                it.type = element.type
                it.value = null
                it.operator.label = it.hotTags[0].label
                it.operator.value = it.hotTags[0].value
                it.hotTags[0].check = true
                if(it.type==='select'){
                     it.select = element.select
                }

           }
       });


  }
add(j,it){
    j.isLogic = true
    let obj = {
           logic: {label:"并且",value:'AND'},
            operator: {label:"=",value:'EQ'},
            field: null,
            value:null,
            hotTags:[],
            type:'input',
            select:[],
            isLogic:false
        }
    it.push(obj)

  console.log(this.universalStar)
}
objAdd(obj,universal,index?){

    if(index!=undefined){
         obj.isLogic = false;
         this.universal.splice(index, 1);
         this.universal[this.universal.length-1]['isLogic'] = false;
    }else{

         obj.isLogic = true;
    let it =  {
        field: this.headerDate,
       logic:  {label:"并且",value:'AND'},
       isLogic:false,
       queryList: [{
           logic: {label:"并且",value:'AND'},
            operator: {label:"=",value:'EQ'},
            field: null,
            value:null,
            hotTags:[],
            type:'input',
            select:[],
            isLogic:false
        }]

    }
    this.universal.push(it)
    }

}
getData(){

                let obj =[];
                for(let i = 0; i <  this.universal.length;i++){
                         let query = []
                    for(let j = 0; j <  this.universal[i].queryList.length;j++){
                         let str = []
                         if(this.universal[i].queryList[j]['type'] === 'date'){

                               this.universal[i].queryList[j]['value'] = moment(this.universal[i].queryList[j]['value']).format('YYYY-MM-DD');
                         }
                        str.push(this.universal[i].queryList[j]['field'], this.universal[i].queryList[j]['value'])
                      query.push({logic:this.universal[i].queryList[j]['logic']['value'],operator:this.universal[i].queryList[j]['operator']['value'],params:str})
                    }
                      obj.push({logic:this.universal[i].logic.value,queryList:query})

                }
            this.getQuery.emit(obj);
}


        // 重置查询
        reset(){

            this.universal = this.universalStar;
            this.resetButton.emit('rs');
        }
// saveSearch(){
//   localStorage.setItem('search',JSON.stringify(this.universal));
//    this.nznot.create('success','保存成功',  '');
// }
}
